//
//  HLEmojiBarView.m
//  YueYue
//
//  Created by hulu001 on 2023/6/13.
//

#import "HLEmojiBarView.h"

@interface HLEmojiBarView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UIImageView *nodataView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray<HLEmojiModel *>* arrayData;

@end

@implementation HLEmojiBarView


-(instancetype)initWithFrame:(CGRect)frame
{
    
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{

    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.bottom.mas_equalTo(0);
    }];
    
    self.nodataView = [[UIImageView alloc] init];
    _nodataView.image = UIImageMake(@"no_data_search");
    _nodataView.hidden = YES;
    [self addSubview:_nodataView];
    [_nodataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    [self requestEmojiHot];
}

-(UICollectionView *)collectionView{
    
    if (!_collectionView) {
        
        CGFloat itemW = (SCREEN_WIDTH - 48)/4.0;
        CGFloat itemH = itemW;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(itemW, itemH);
        layout.minimumLineSpacing = 8;
        layout.minimumInteritemSpacing = 8;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        layout.headerReferenceSize = CGSizeMake(0, 0);
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = UIColor.whiteColor;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.pagingEnabled = YES;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerClass:HLEmojiCollectionCell.class forCellWithReuseIdentifier:@"HLEmojiCollectionCell"];
    }
    
    return _collectionView;
}


#pragma mark - UICollectionViewDataSource

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrayData.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HLEmojiCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HLEmojiCollectionCell" forIndexPath:indexPath];
    cell.model = self.arrayData[indexPath.row];
    return cell;
}

#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HLEmojiModel *model = self.arrayData[indexPath.row];
    if (self.callback){
        self.callback(model.origin.gif, model.image);
    }
    
}

-(void)requestEmojiHot{
    
    NSDictionary *parameters = @{@"pageNum":@(1), @"pageSize":@(100)};
    
//    [YDHttpMgr GET:@"/shanmeng/hot" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//        NSArray *arr = responseObject[@"data"];
//        self.arrayData = [YDEmojiModel mj_objectArrayWithKeyValuesArray:arr];
//        [self.collectionView reloadData];
//        
//    } bussinessError:^(NSString * _Nullable errorCode, NSString * _Nullable msg) {
//        [QMUITips showInfo:msg inView:self];
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        [QMUITips showInfo:error.localizedDescription inView:self];
//    }];
}


@end
